<?php

class M_products extends CI_Model
{
	public function addProduct($productData)
	{
		$productData['created'] = NOW;
		$productData['updated'] = NOW;
		$this->db->insert('products', $productData);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function getProduct($productid)
	{
		$q = $this->db->get_where('products', array('id' => $productid));
		return $q->num_rows() > 0 ? $q->row() : FALSE;
	}
	
	public function getProducts()
	{
		$q = $this->db->get('products');
		return $q->num_rows() > 0 ? $q->result() : FALSE;
	}
	
	public function setProductFeatured($productid)
	{
		$this->db->where('id', $productid)->update('products', array('featured' => 'yes', 'updated' => NOW));	
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function unsetFeatured($productid)
	{
		$this->db->where('id', $productid)->update('products', array('featured' => '', 'updated' => NOW));	
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function updateProduct($productData, $productid)
	{
		$productData['updated'] = NOW;
		$this->db->where('id', $productid)->update('products',$productData);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function deleteProduct($productid)
	{
		$this->db->delete('products', array('id' => $productid));
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	public function getFeatured()
	{
		$q = $this->db->get_where('products', array('featured' => 'yes'));
		return $q->num_rows() > 0 ? $q->result() : FALSE;
	}
	
	public function addOrder($order, $order_data) {

		//die(count($order_data));
		
		$this->db->insert('orders', $order);
		$last_id = $this->db->insert_id();
		
		$order_details = array();
		foreach($order_data as $odd){
			
			$o['o_id'] = $last_id;
			$o['product_id'] = $odd['product_id'];
			$o['qty'] = $odd['qty'];
			$o['total'] = $odd['total'];
			$order_details[] = $o;
		}
		
		$this->db->insert_batch('order_details', $order_details);
		
		return TRUE;
	}
	
	public function getCustomerPurchases($customer_id){
		$q = "SELECT * FROM orders WHERE user_id = '$customer_id' ";
		
		$r = $this->db->query($q);
		
		return $r->num_rows() >= 1 ? $r->result() : FALSE;
	}
	
	public function getPurchasedItems($order_id, $user_id){
		$q = "SELECT order_id, user_id, items, orders.total as ototal, qty, order_details.total as od_total, name, price FROM orders 
LEFT JOIN order_details ON orders.order_id = order_details.o_id 
LEFT JOIN products ON order_details.product_id = products.id
WHERE orders.order_id = '$order_id' AND orders.user_id = '$user_id'";

		$r = $this->db->query($q);
		
		return $r->num_rows() >= 1 ? $r->result() : FALSE;
	}
	
	public function getOrders(){
		$q = "SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id";	
		
		$r = $this->db->query($q);
		
		return $r->num_rows() >= 1 ? $r->result() : FALSE;
	}
	
	public function getOrder($order_id, $user_id){
		$q = "SELECT * FROM orders LEFT JOIN users ON users.id = orders.user_id WHERE orders.order_id = '$order_id' AND users.id = '$user_id' ";	
		$r = $this->db->query($q);
		return $r->num_rows() >= 1 ? $r->row() : FALSE;
	}
	
	public function setOrderStatus($order_id, $action){
		$q = $this->db->where('order_id', $order_id)->update('orders', array('status' => $action));
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function deleteOrders($order_id){
		$this->db->delete('orders', array('order_id' => $order_id));
		$this->db->delete('order_details', array('od_id' => $order_id));
		return TRUE;
	}
}

?>